<?php
/**
 * 提取adzoneid
 * @param $pid
 * @return string
 */
function get_adzoneid($pid)
{
    //$pid = 'mm_131870844_44674332_497770351';
    $adzoneid = substr($pid,strrpos($pid,"_")+1);
    /*    $pid = explode('_', $pid);
        $adzoneid = $pid[3];*/
    return $adzoneid;
}
/**
 * 优惠券信息处理
 * 如：“满16元减10元”
 * @param $html
 * @param $star
 * @param $end
 * @return int
 */
function get_word($html,$star,$end)
{
    $word = 0;
    $pat = '/'.$star.'(.*?)'.$end.'/s';
    if(!preg_match_all($pat, $html, $mat)) {
    }else{
        $word = $mat[1][0];
    }
    return $word;
}
/**
 * 不带LOGO
 * 生成二维码图片
 * 自定义生成二维码 不带logo图
 * $url:链接地址
 * $alias：别名（可不传）
 */
function scerweima2($url='',$alias=''){
    try{
        import('phpqrcode/phpqrcode',EXTEND_PATH);
        $object = new \QRcode();
        $level=3;
        $size=4;
        $pt = 'uploads/code/';
        if (!is_dir($pt)) {
            $r = mkdir($pt);
            if (!$r) {
                return json_encode(['msg' => '创建目录失败', 'ok' => false]);
            }
        }
        $im = 'code/' . date('Ymd', time()) . $alias . '.png';
        $ad = $pt . date('Ymd', time()) . $alias . '.png';
        $errorCorrectionLevel = intval($level);//容错级别
        $matrixPointSize = intval($size);//生成图片大小
        $object->png($url, $ad, $errorCorrectionLevel, $matrixPointSize, 2);
        //return json_encode(['msg'=>$im,'ok'=>true]);
        if(file_exists($ad)) {
            return json_encode(['msg'=>$im,'ok'=>true]);
        }else{
            return json_encode(['msg' => '创建文件失败', 'ok' => false]);
        }

    }catch (\Exception $e){
        $err = $e->getMessage();
        return json_encode(['msg'=>$err,'ok'=>false]);
    }


}

/**
 * 随机生成几个字母
 * @return string
 */
function randLetter(){
    for ($i = 1; $i <= 4; $i++) {
        return chr(rand(97, 122));
    }
}

/**
 * 随机去几个字母数子
 * @param $len
 * @param null $chars
 * @return string
 */
function getRandomString($len, $chars=null)
{
    if (is_null($chars)) {
        $chars = "ABCDEFJHIJKLMNOPQRSTUVWXYZ123456789";
    }
    mt_srand(10000000*(double)microtime());
    for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) {
        $str .= $chars[mt_rand(0, $lc)];
    }
    return $str;
}

/**
 * @param string $y：年
 * @param string $m：月
 * @return array
 */
function mFristAndLast($y="",$m=""){
    if($y=="") $y=date("Y");
    if($m=="") $m=date("m");
    $m=sprintf("%02d",intval($m));
    $y=str_pad(intval($y),4,"0",STR_PAD_RIGHT);

    $m>12||$m<1?$m=1:$m=$m;
    $firstday=strtotime($y.$m."01000000");
    $firstdaystr=date("Y-m-01",$firstday);
    $lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));

    return array("firstday"=>$firstday,"lastday"=>$lastday);
}

/**
 * php二维数组删除某个键值相同的数组
 * @param $arr
 * @param $key
 * @return mixed
 */
function second_array_unique_bykey($arr, $key){
    $tmp_arr = array();
    foreach($arr as $k => $v)
    {
        if(in_array($v[$key], $tmp_arr))   //搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
        {
            unset($arr[$k]); //销毁一个变量  如果$tmp_arr中已存在相同的值就删除该值
        }
        else {
            $tmp_arr[$k] = $v[$key];  //将不同的值放在该数组中保存
        }
    }
    //ksort($arr); //ksort函数对数组进行排序(保留原键值key)  sort为不保留key值
    return $arr;
}



function down_images($url,$rand) {


    $header = array("Connection: Keep-Alive", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Pragma: no-cache", "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3", "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:29.0) Gecko/20100101 Firefox/29.0");

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    //curl_setopt($ch, CURLOPT_HEADER, $v);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');

    $content = curl_exec($ch);

    $curlinfo = curl_getinfo($ch);

//    print_r($curlinfo);
//   die;

    //关闭连接

    curl_close($ch);

    if ($curlinfo['http_code'] == 200) {

        if ($curlinfo['content_type'] == 'image/jpeg') {

            $exf = '.jpg';

        } else if ($curlinfo['content_type'] == 'image/png') {

            $exf = '.png';

        } else if ($curlinfo['content_type'] == 'image/gif') {

            $exf = '.gif';

        }

        //存放图片的路径及图片名称  *****这里注意 你的文件夹是否有创建文件的权限 chomd -R 777 mywenjian

        $timeFile = date('Ymd',time()); // 按照时间生成目录

        $pic1 = $rand.substr(md5(time()),5,10).$exf;
        $pic =  ROOT_PATH.'public/files/'.$timeFile;
        $path1 = $pic .'/'.$pic1;
        if(!is_dir($pic)){
            $r = mkdir($pic,0777,true);
            chmod($pic,0777);
            if(!$r){
                return json(['msg'=>'创建目录失败！','ok'=>false]);
            }
        }

        $res = file_put_contents($path1, $content);
        //$res = file_put_contents($filename, $content);
        //同样这里就可以改为$res = file_put_contents($path1, $content);
        //echo $path1;
        echo $res;
        if($res){
          //  echo $res;
            return $pic1;
        }

    }

// 删除uploads 图片
    function remove_img($file){
        $path = ROOT_PATH . 'public' . DS . 'uploads' .DS;
        return unlink($path.$file);
    }



}


